package com.trace.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trace.app.entity.ResponseResult;
import com.trace.app.entity.ShipAddress;
import com.trace.app.entity.consts.AppHttpCodeEnum;
import com.trace.app.entity.consts.SysTemConst;
import com.trace.app.entity.vo.PageVo;
import com.trace.app.mapper.ShipAddressMapper;
import com.trace.app.service.ShipAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 收货地址(ShipAddress)表服务实现类
 *
 * @author lionkliu
 * @since 2024-04-02 19:08:53
 */
@Slf4j
@Service("shipAddressService")
public class ShipAddressServiceImpl extends ServiceImpl<ShipAddressMapper, ShipAddress> implements ShipAddressService {

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<ShipAddress> queryWrapper = new LambdaQueryWrapper<>();
        Page<ShipAddress> page = page(new Page<>(pageNum, pageSize), queryWrapper);
        PageVo<ShipAddress> pageVo = new PageVo<>();
        pageVo.setResult(page.getRecords());
        pageVo.setTotal(page.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(ShipAddress shipAddress) {
        return save(shipAddress) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(ShipAddress shipAddress) {
        LambdaUpdateWrapper<ShipAddress> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(ShipAddress::getId, shipAddress.getId())
                .set(ShipAddress::getAddress, shipAddress.getAddress())
                .set(ShipAddress::getName, shipAddress.getName())
                .set(ShipAddress::getPhone, shipAddress.getPhone());
        update(null, updateWrapper);
        return ResponseResult.okResult();
    }

    @Transactional
    @Override
    public ResponseResult setDefault(ShipAddress shipAddress) {
        ShipAddress address = getById(shipAddress.getId());
        Integer rank;
        LambdaUpdateWrapper<ShipAddress> updateWrapper = new LambdaUpdateWrapper();

        if (address != null) {
            rank = address.getRank();
            updateWrapper.eq(ShipAddress::getId, shipAddress.getId())
                    .set(ShipAddress::getRank, SysTemConst.DEFAULT_ADDRESS);
        }
        update(null, updateWrapper);


        LambdaQueryWrapper<ShipAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShipAddress::getUserId, shipAddress.getUserId())
                .eq(ShipAddress::getRank, SysTemConst.DEFAULT_ADDRESS);
        List<ShipAddress> list = list(queryWrapper);
        log.info("list:{}", list);
        list.stream().forEach(item -> {
            if (address != null && !Objects.equals(item.getId(), address.getId())) {
                LambdaUpdateWrapper<ShipAddress> updateWrapper2 = new LambdaUpdateWrapper<>();
                updateWrapper2.eq(ShipAddress::getId, item.getId())
                        .set(ShipAddress::getRank, SysTemConst.NO_DEFAULT_ADDRESS);
                update(null, updateWrapper2);
            }

        });


        return ResponseResult.okResult();
    }
}

